<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8" %>
<%@ include file="../include/laypublic.jsp" %>
<!DOCTYPE html>
<html>
<head>
  <title>起诉案件申请单新增</title>
</head>
<script type="text/html" id="proof-list-tr" >
<tr id="proofListRow-{0}"  > 
<td><span class="label" >{0}</span><input type="hidden" value="{0}" name="proofShowIdx" /></td> 
<td><input type="text" value="{1}" name="proofName" autocomplete="off" lay-verify="required" class="layui-input"/></td>
<td><input type="text" value="{2}" name="proofTarget" autocomplete="off" lay-verify="required" class="layui-input " /></td>
<td><input type="text" value="{3}" name="proofAttribute" autocomplete="off" lay-verify="required" class="layui-input " /></td>
<td><input type="text" value="{4}" onblur="isInt(this);" name="proofNumOfPage" autocomplete="off" lay-verify="required|intNumber" class="layui-input " /></td>
<td><div class="layui-upload" ><a id="{6}-upload-more" name="showIdxAtt" href="javascript:void(0);" >上传</a>
	<div class="layui-upload-list" id="{6}-upload-more-list" ></div></div>
</td>							
<td class="tg-c" > 
<button type="button" class="layui-btn layui-btn-sm del-row" ><i class="layui-icon">&#xe640;</i></button>												
<button type="button" class="layui-btn layui-btn-sm add-row" ><i class="layui-icon">&#xe654;</i></button>
</td>		
</tr>
</script>
<style>	
	a{
		color:blue;
		text-decoration: none; 
  		border-bottom: 1px solid blue; 
	}
	.tg-c {
		text-align: center;
	}
	.contract-box {
		width: 100%;
		min-height: 100px;
	}
	
	.contract-money {
		width: 94%;
		margin: 0px auto;
	}
	
	.contract-money .layui-table {
		margin: 0;
	}
	
	.contract-money th {
		text-align: center;
		height: 38px;
	}
	
	.contract-money .label {
		display: block;
		width: 45px !important;
		margin-left: 15px;
	}
	.fieldset{
		margin-top: 5px;
		margin-bottom: 0px
	}
	.layui-legend{
		background-color:#5fb878;
		font-size:15px !important;
		width:140px;
	}
	.layui-form-item {
	    margin-bottom: 5px;
	    clear: both;
	    *zoom: 1;
	}
	.layui-form-label{width: 130px;}
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
	    <div class="layui-tab-item layui-show">
	    	<div style="padding-top:15px;">&nbsp;</div>
			<form class="layui-form" id="addInfo_form" lay-filter="add_info_from">
				<fieldset class="layui-elem-field layui-field-title fieldset"  id="baseInfo">
				  <legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">案件基础信息  ></legend>
				  <div style="margin-top:20px" >				  
				  	<div class="layui-form-item" >
				  		<div class="layui-inline">
					      	<label class="layui-form-label">申请公司</label>
					      	<div class="layui-input-inline">
					      		<input type="hidden" name="hrOrgid" value="${bindOrgId}">
								<input type="text" value="${bindOrgName}" style="border: 0px;" readonly="readonly" class="layui-input">
					      	</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label" >申请部门</label>
				      		<div class="layui-input-inline" >
				      			<input type="hidden" name="hrDeptid" value="${bindDeptId}">
								<input type="text" value="${bindDeptName}" style="border:0px;" readonly="readonly" class="layui-input">
				      		</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label">申请人</label>
				      		<div class="layui-input-inline">
				        		<input type="hidden" name="hrStaffApplyid" readonly="readonly" value="${SESSION_KEY_USER_DETAIL.hrStaffId }">
				        		<input type="text" name="staffname" value="${staffName }" readonly="readonly" style="border:0px" class="layui-input">
				      		</div>
					    </div>
				    </div>
				    
				  	<div class="layui-form-item">
				  		<div class="layui-inline">
					      	<label class="layui-form-label">案件名称</label>
					      	<div class="layui-input-inline">
					      		<input type="text" id="id_lawcaseName" name="lawcaseName" maxlength="50"
									placeholder="请输入案件名称" autocomplete="off" lay-verify="required" class="layui-input">					        
					      	</div>
					    </div>
					    <div class="layui-inline">
							<label class="layui-form-label">立案日期</label>
							<div class="layui-input-inline">
								<input type="text" class="layui-input" id="id_setDate" name="setDate" autocomplete="off" lay-verify="required" placeholder="立案日期">
							</div>
						</div>
				    	<div class="layui-inline">
							<label class="layui-form-label">标的额（万元）</label>
							<div class="layui-input-inline">
								<input type="text" id="id_targetAmount" name="targetAmount" maxlength="11" onblur="isNumberAndFloat1(this);arabiaToCN(this,2);"
									placeholder="请输入标的额" autocomplete="off" lay-verify="required|floatAmount" class="layui-input" >
							</div>
						</div>
				    </div>		

				    <div class="layui-form-item">
				  		<div class="layui-inline">
					      	<label class="layui-form-label" >对方当事人</label>
					      	<div class="layui-input-inline" >
					        	<input type="text" class="layui-input" name="sideParty" maxlength="50" placeholder="请输入对方当事人" autocomplete="off" lay-verify="required"  >
							</div>
					    </div>
					    <div class="layui-inline">
							<label class="layui-form-label" >原告</label>
							<div class="layui-input-inline" >
								<input type="text" class="layui-input" name="plaintiff" maxlength="50" placeholder="请输入原告"   autocomplete="off" lay-verify="required"  >
							</div>
						</div>
					    <div class="layui-inline">
							<label class="layui-form-label" >被告</label>
							<div class="layui-input-inline" >
								<input type="text" class="layui-input" name="defendant" maxlength="50" placeholder="请输入被告"   autocomplete="off" lay-verify="required"  >
							</div>
						</div>
				    </div>		
				    		    		    
				    <div class="layui-form-item">
						<div class="layui-inline">
					    	<label class="layui-form-label">案由</label>
					    	<div class="layui-input-inline">
							    <select name="causeType" lay-verify="required" lay-filter="add_select_causeType">
						      			<option value=""  >请选择</option>
						      			<option value="01" >建筑工程合同纠纷</option>
						      			<option value="02" >买卖合同纠纷</option>
						      			<option value="03" >租赁合同纠纷</option>
						      			<option value="04" >房屋买卖合同纠纷</option>
						      			<option value="05" >设计合同纠纷</option>
						      			<option value="06" >营销合同纠纷</option>
						      			<option value="07" >借款合同纠纷</option>
						      			<option value="08" >投/融 资合同纠纷</option>
						      			<option value="09" >抵押/质押合同纠纷</option>
						      			<option value="10" >担保合同纠纷</option>
						      			<option value="11" >保险合同纠纷</option>
						      			<option value="12" >设备安装合同纠纷</option>
						      			<option value="13" >加工承揽合同纠纷</option>
						      			<option value="14" >服务合同纠纷</option>
						      			<option value="99" >其他</option>
								</select>
							</div>
					    </div>
					    <div class="layui-inline">
							<label class="layui-form-label">承办人</label>
							<div class="layui-input-inline">
								<input type="text" name="underTaker" maxlength="50" placeholder="请输入承办人" autocomplete="off" lay-verify="required" class="layui-input">
							</div>
						</div>	
					</div>			  	
		    		
		    		<div class="layui-form-item">
						<label class="layui-form-label">起诉原因</label>
						<div class="layui-input-block">
							<textarea name="sueReason" class="layui-textarea" maxlength="1000"
								lay-verify="required" style="width: 899px; height: 90px;"></textarea>
						</div>
					</div>
					
					
		  		</div>
			</fieldset>
			<fieldset class="layui-elem-field layui-field-title fieldset"  id="proofInfo">
				<legend class="layui-btn layui-legend" style="text-align:left;cursor:default;" >证据清单  ></legend>
				<div style="margin-top:20px">																					
					<div class="contract-box" id="proof-list">
        				<div class="contract-money">
            				<table class="layui-table">
			                    <colgroup>
			                        <col width="90">
			                        <col width="300" >
			                        <col width="300" >
			                        <col width="300" >
			                        <col width="300" >
			                        <col width="300" >
			                        <col width="200" >
			                    </colgroup>
								<thead>
									<tr>
										<th>序号</th>
										<th>证据名称</th>
										<th>证明对象</th>
										<th>证据属性</th>
										<th>页数</th>
										<th>附件</th>
										<th></th>
									</tr>
								</thead>
								<tbody >
			                	</tbody>
		                	</table>
               			</div>                									
					</div>
				</div>
			</fieldset>			
			<fieldset class="layui-elem-field layui-field-title fieldset">
			<legend class="layui-btn layui-legend" style="text-align: left; cursor: default;">附件信息 ></legend>
			<div style="margin-top: 20px;">
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 350px;">支持pdf、excel、word、图片资料，可上传多个</label>
				</div>										
					<div class="layui-form-item">
						<label class="layui-form-label">起诉状</label>
						<div class="layui-input-block">
							<div class="layui-upload">
								<button type="button" class="layui-btn" id="1-upload-more" >上传</button>
								<div class="layui-upload-list" id="1-upload-more-list" ></div>
							</div>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">其他附件（选填）</label>
						<div class="layui-input-block">
							<div class="layui-upload">
								<button type="button" class="layui-btn" id="2-upload-more" >上传</button>
								<div class="layui-upload-list" id="2-upload-more-list" ></div>
							</div>
						</div>
					</div>
				</div>
			</fieldset>
			
			<div class="layui-form-item" style="position: fixed ! important; right: 30px; bottom:45%;z-index:10000;">
				<div class="layui-input-block">
		      		<button class="layui-btn layui-bg-blue layui-btn-normal layui-btn-lg  layui-btn-radius" type="button" lay-submit lay-filter="addInfo_form_submit" id="addInfo_form_submit">
		      		<i class="layui-icon layui-icon-ok layuiadmin-button-btn" ></i>
		      		提交
		      		</button>
		      	</div>
		    </div>
		</form>
  		</div>
	</div>
</div>
<script>
  	String.prototype.format = function () {
		var model = arguments;
	    return this.replace(/\{(\d+)\}/g, function (s, i) { 
	        return !!model[i]?model[i]:'';
	    });
	}
	//静态资源路径前缀
	var staticPath = '${fileHttpPre}';
	var stateJSON = <infopub:dicJson groupNo="A00010028" />;
	
  layui.config({
    base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'set', 'upload', 'element', 'laydate'], function(){
    var $ = layui.$
    ,form = layui.form
    ,element = layui.element
    ,laydate = layui.laydate
    ,upload = layui.upload;
    
 	// 正则校验
	form.verify({
		//验证汉字
	    CHS:[
	    	/^[\u0391-\uFFE5]+$/
	    	,'只能输入汉字'
	  	],
	  	//大于0的正整数
	  	intNumber:[
			/^[1-9]\d*$/
			,'请输入正确的数字'
	  	],
	  	//数字只能有两位小数
	  	floatNumber:[
			/^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
			,'请输入正确的数字'
	  	],
	  	//数字大于等于0，并只能有两位小数
	  	floatAmount:[
			/^([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
			//var pattern=/^((\d+\.\d*[1-9]\d{1})|(\d*[1-9]\d*\.\d{2}))$/; 
			,'请输入正确的数字'
	  	]
	});
 	
	laydate.render({
        elem: '#id_setDate'
        ,trigger: 'click'
    });

  	proofListDataBind($,upload);
	
  	for(var i=1;i<=2;i++)uploadRender(i,upload,$);
  	
   	//监听提交
  	form.on('submit(addInfo_form_submit)', function(data){
  		//判断是否上传了起诉状
		if ($("input[name=fileAtts1]").length < 1) {
			layer.msg("起诉状未上传", {
				icon : 0
			});
			return;
		}
  		$("#addInfo_form_submit").removeAttr("lay-submit").removeClass("layui-bg-blue").addClass("layui-btn-disabled");
  		layer.load();
       	//提交 Ajax 成功后，静态更新表格中的数据
       	$.ajax({
           	type : 'post',
			url : basePath + '/sueLawcase/save',
           	data: $('#addInfo_form').serialize(),
			success:function(result){
				if(result.success){
					layer.closeAll('loading'); //关闭loading
					layer.msg('提交单据成功~',{icon: 1});
					//两秒后关闭当前页面
					setTimeout(function(){
						/**
						 * 查找父菜单，并刷新
						 */
						var topDoc = $(window.top.document);
						var path = basePath + '/sueLawcase/list';
						// 获取页面名称
						var name = topDoc.find(".layui-nav-item [lay-href^='" + path + "']").text();
						if(name == '' || name == null){
							name = '起诉案件申请单';
						}
						window.parent.layui.index.openTabsPage(path, name);
						var iframe = window.parent.layui.admin.tabsBody(window.parent.layui.admin.tabsPage.index).find(".layadmin-iframe");
						iframe[0].contentWindow.location.reload(true);
						
						/**
						 * 关闭当前页
						 */
						var currPath = basePath + '/sueLawcase/add';
						var layid = topDoc.find("li[lay-id^='" + currPath + "']").attr('lay-id');
						window.parent.layui.element.tabDelete('layadmin-layout-tabs', layid);//关闭当前页
				        }, 2000);
				}else{
					layer.closeAll('loading'); //关闭loading
  					$("#addInfo_form_submit").removeClass("layui-btn-disabled").addClass("layui-bg-blue").attr("lay-submit","");
					layer.msg(result.msg,{icon: 7});
			  	}
			}
       	});
   	});  
   	
 	// data-type事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });  	
  });
  
  var proofListDataBind = function($,upload){	
		var proofListData = [{proofName:'',proofTarget:'',proofAttribute:'',proofNumOfPage:0,showIdxAtt:''}];			
		var $table = $('#proof-list').find('tbody');
		var template = $('#proof-list-tr').html();	
		$table.empty();
		$table.on('click','.add-row',function(){									
			var $row = $(this).parents('tr');
			var rowId = parseInt($row.find('.label').text())+1;
			var eid = eightS4()
			var newRowHtml = template.format(rowId,null,null,null,null,null,eid);
			var $newRow = $(newRowHtml);
			$row.after($newRow);
			uploadRender(eid,upload,$);	
		
			var newIndex = $newRow.index(); 
			$table.find('tr:gt('+newIndex+')').each(function(){
				var $row = $(this);
				var rowId= parseInt($row.find('.label').text())+1;
				$row.find('.label').text(rowId).end().find('input[type=hidden]').val(rowId);
			});				
		}).on('click','.del-row',function(){						
			var $row = $(this).parents('tr');var index = $row.index()-1;
			$row.remove();			 
			$table.find('tr:gt('+index+')').each(function(){
				var $row = $(this);
				var rowId= parseInt($row.find('.label').text())-1;
				$row.find('.label').text(rowId).end().find('input[type=hidden]').val(rowId);
			});			
		});	
		
		for(var i=0,j=proofListData.length;i<j;i++){
			var eid = eightS4();
			var html=[],rowId=i+1;
			var item = proofListData[i];
			var rowHtml = template.format(rowId,item.proofName,item.proofTarget,item.proofAttribute,item.proofNumOfPage,item.showIdxAtt,eid);
			var $rowHtml = $(rowHtml);
			if(i==0){
				$rowHtml.find('.del-row').remove();
			}
			$table.append($rowHtml);
			uploadRender(eid,upload,$);	
		}
	}
  
  
   function eightS4() {
      function S4() {
          return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
      }
      return (S4()+S4()+""+S4()+""+S4()+""+S4()+""+S4()+S4()+S4()).toLocaleUpperCase();
   }
	
	//删除文件
   function del(index){
	 layui.$("#upload_img_"+index).remove();
   }
  
	//根据url查看文件
	function showImg(url){
		var openWin = window.open(url,'','left=410,height=768, width=1024, top=200,  toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
	}
	
	//'请输入正确数字，且必须为正整数！'
	function isInt(str) {		
		if (str.value == '' || str.value == null) {
			return false;
		}
		var rgx = /^[1-9]\d*$/;
		var boolrgx = rgx.test(str.value);// true 
		if (boolrgx) {
			return true;
		} else {
			str.value =0;
			layer.msg('请输入正确数字，且必须为正整数！');
			return false;
		}
	}
	
	//'请输入正确数字，且保留两位小数！'
	function isNumberAndFloat1(str) {
		var $ = layui.$, form = layui.form
		if (str.value == '' || str.value == null) {
			return;
		}
		var rgx = /^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/;
		var boolrgx = rgx.test(str.value);// true 
		if (boolrgx) {
			return true;
		} else {
			str.value =0;
			layer.msg('请输入正确数字，且保留两位小数！');
			return false;
		}
	}
	
	//子项附件
  	var uploadRender = function(id,upload,$) {
		upload.render({
			elem : '#' + id + '-upload-more'
			,url : basePath + '/sueLawcase/uploadAtt/'
			,data : {
				types : id
			}
			,multiple : true
			,accept : 'file'
			,acceptMime : 'application/zip,application/x-rar-compressed,image/jpeg,image/png,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/pdf'
			,before : function(obj) {
				layer.load();
			}
			,done : function(res) {
				if (res.code > 0) {
					layer.closeAll('loading');
					return layer.msg(res.msg, {
						icon : 7
					});
				}
				
				if (res.entity != null && res.entity.length > 0) {
					for ( var i in res.entity) {						
						var sueLawcase = res.entity[i];
						var arr = sueLawcase.split("|");
						var imgSrc = "";
						var suffix = arr[1].substring(arr[1].lastIndexOf(".") + 1, arr[1].length);
						if (suffix == 'jpg' || suffix == 'png') {
							imgSrc = "${fileHttpPre}/" + arr[3];
						} else {
							imgSrc = "${ctx}/resources/images/" + suffix + ".jpg";
						}
						var flag = false;
						if(id.length==32){
							flag = false;
						}else{
							flag = true;
						}
						
						var inputStr = flag==false?'<input name="showIdxAtt" value="" type="hidden" /><input style="display:none;" value="'+sueLawcase+'"  name="proofFileAtts" />':'<input type="hidden" value="'+sueLawcase+'"   name="fileAtts'+id+'" />';
						var $uploadList = $('#' + id + '-upload-more-list');
						$uploadList.append('<div class="layui-upload-img-div" id="upload_img_'+arr[2]+'" ><img src="'+imgSrc+'" title="'+ arr[1] +'" class="layui-upload-img" /><div class="mask" ><a href="javascript:void(0)" onclick="showImg(\''
							+ "${fileHttpPre}"
							+ arr[3]
							+ '\');" >查看</a><a href="javascript:void(0)" onclick="del(\''
							+ arr[2]
							+ '\')"; >删除</a></div><p>'
							+ arr[1]
							+ '</p>'+inputStr+'</div>');
						
						var $tr = $uploadList.parents('tr');
						var rowId = $tr.find('.label').text();											
						$uploadList.find('input[name="showIdxAtt"]').val(rowId);					
					}
				}
			}
			,allDone : function(obj) {
				layer.closeAll('loading');
			}
			,error : function() {
				layer.closeAll('loading');
				layer.msg("上传失败，请稍后再试~", {
					icon : 7
				});
			}
		});
	}
  </script>
</body>
</html>